import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
    {
        path: '',
        name: 'Login',
        component: () => import('../views/login/index.vue')
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('../views/login/index.vue')
    },
    {
        path: '/home',
        name: 'Home',
        redirect: "/home/index",
        component: () => import('../views/home/index.vue'),
        children: [
            {
                path: '/home/index',
                name: 'home_index',
                component: () => import('../views/home/home/index.vue')
            },
            {
                path: '/home/gallery',
                name: 'home_gallery',
                component: () => import('../views/home/home/gallery.vue')
            },
            {
                path: '/home/search',
                name: 'home_search',
                component: () => import('../views/home/home/search.vue')
            },
            {
                path: '/home/meaty',
                name: 'home_meaty',
                component: () => import('../views/home/home/meaty.vue')
            },
            {
                path: '/home/curing',
                name: 'home_curing',
                component: () => import('../views/home/home/curing.vue')
            },
            {
                path: '/home/information',
                name: 'home_information',
                component: () => import('../views/home/home/information.vue')
            },
            {
                path: '/home/soil',
                name: 'home_soil',
                component: () => import('../views/home/home/soil.vue')
            },
            {
                path: '/home/save',
                name: 'home_save',
                component: () => import('../views/home/home/save.vue')
            },
            {
                path: '/home/news',
                name: 'home_news',
                component: () => import('../views/home/home/news.vue'),
                redirect: "/home/news/img",
                children: [
                    {
                        path: '/home/news/img',
                        name: 'home_news_img',
                        component: () => import('../views/home/home/news_img.vue')
                    },
                    {
                        path: '/home/news/meaty',
                        name: 'home_news_meaty',
                        component: () => import('../views/home/home/news_meaty.vue')
                    },
                    {
                        path: '/home/news/other',
                        name: 'home_news_other',
                        component: () => import('../views/home/home/news_other.vue')
                    },
                ]
            },
        ]
    },

]





const router = new VueRouter({
    routes,
    // base: 'bff', // nginx配置
    mode: 'history', // 去掉url中的#
})
router.beforeEach((to, from, next) => {
    // console.log(to);

    let isLogin = JSON.parse(localStorage.getItem("user"))
    // //我在这里模仿了一个获取用户信息的方法
    // let isLogin = window.sessionStorage.getItem('userInfo');
    if (isLogin) {

        //如果用户信息存在则往下执行。
        if (to.path === '/login' || to.path == "/") {
            next('/home')
        } else {
            next()
        }

    } else {
        //如果用户token不存在则跳转到login页面
        // console.log(to);

        if (to.path === '/login' || to.path === '/home/index' || to.path === '/home/gallery' || to.path === '/home/meaty' || to.path === '/home/curing' || to.path === '/home/information' || to.path === '/home/soil') {

            next()
        } else {
            next('/login')
        }
    }
    next()
})
export default router
